<template>
  <Form ref="form" :model="form" :rules="rule" :label-width="100">
    <FormItem label="登录名" prop="userName">
      <Input v-model="form.userName" placeholder=""></Input>
    </FormItem>
    <FormItem label="密码" prop="password">
      <Input v-model="form.password" placeholder=""></Input>
    </FormItem>
    <FormItem label="确认密码" prop="confirmPassword">
      <Input v-model="form.confirmPassword" placeholder=""></Input>
    </FormItem>
    <FormItem label="姓名" prop="realName">
      <Input v-model="form.realName" placeholder=""></Input>
    </FormItem>
    <FormItem label="手机号" prop="mobilePhone">
      <Input v-model="form.mobilePhone" placeholder=""></Input>
    </FormItem>
    <FormItem label="联系电话" prop="telephone">
      <Input v-model="form.telephone" placeholder=""></Input>
    </FormItem>
    <FormItem label="性别" prop="sex">
      <RadioGroup v-model="form.sex">
          <Radio :label="1">男</Radio>
          <Radio :label="2">女</Radio>
      </RadioGroup>
    </FormItem>
    <FormItem label="是否锁定" prop="isLocked">
      <RadioGroup v-model="form.isLocked">
          <Radio :label="1">是</Radio>
          <Radio :label="0">否</Radio>
      </RadioGroup>
    </FormItem>
  </Form>
  </template>
<script>
import { save, update, get } from '@/api/sys/sys.admin.service.js'

export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      form: {
        id: 0,
        userName: '',
        password: '',
        confirmPassword: '',
        realName: '',
        mobilePhone: '',
        telephone: '',
        isLocked: 1,
        sex: 1
      },
      rule: {
        userName: [
          { required: true, message: '登录名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: !this.isEdit, message: '密码不能为空', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: !this.isEdit, message: '确认密码不能为空', trigger: 'blur' }
        ],
        realName: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        mobilePhone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        telephone: [
          { required: false, message: '联系电话不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    if (this.isEdit) {
      get({
        id: this.params.id
      }).then(res => {
        this.form = this.$util.copy(res.data, this.form)
      })
    }
  },
  methods: {
    submit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.isEdit) {
              update(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            } else {
              delete this.form.id
              save(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            }
          } else {
            reject(new Error('error'))
          }
        })
      })
    }
  }
}
</script>
